package com.deyuanyun.pic.ctrl.aop;

import com.deyuanyun.pic.ctrl.excelImport.PicCtrlExinspAcMiorRecordExcel;
import com.deyuanyun.pic.ctrl.excelImport.PicCtrlExinspCptestStationExcel;
import com.deyuanyun.pic.ctrl.excelImport.PicCtrlExinspDcMiorRecordExcel;
import com.deyuanyun.pic.ctrl.excelImport.PicCtrlExinspInsedJointExcel;
import com.deyuanyun.pic.ctrl.excelImport.PicCtrlExinspOcgradeExcel;
import com.deyuanyun.pic.ctrl.excelImport.PicCtrlExinspOclpointExcel;
import com.deyuanyun.pic.ctrl.excelImport.PicCtrlExinspSaalAnodeExcel;
import com.deyuanyun.pic.ctrl.service.pushmsg.CtrlPushMsgCons;
import com.deyuanyun.pic.ctrl.service.pushmsg.ExinspAcDcMiorRecordPushMsg;
import com.deyuanyun.pic.ctrl.service.pushmsg.ExinspCptestStationPushMsg;
import com.deyuanyun.pic.ctrl.service.pushmsg.ExinspInsedJointSaalAnodePushMsg;
import com.deyuanyun.pic.ctrl.service.pushmsg.ExinspOcgradePushMsg;
import com.deyuanyun.pic.ctrl.service.pushmsg.ExinspOclpointAddNotice;
import com.deyuanyun.pic.settings.aop.BasePushMsgAspect;
import com.deyuanyun.pic.settings.service.msgtemplate.AbstractPushMsgTemplate;
import com.deyuanyun.pic.settings.service.msgtemplate.PushMessage;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  外检项目数据录入后消息推送切面
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-10-11
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
@Service
@Aspect
public class ExinspTaskAspect extends BasePushMsgAspect {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExinspTaskAspect.class);

    /**
     * 外检项目切入点
     * @param
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-10-09
     */
    @Pointcut(value = "@annotation(pushMessage) && target(com.deyuanyun.pic.ctrl.service.ImportService)")
    private void importServicePointcut(PushMessage pushMessage) {

    }

    /**
     * 在{@link com.deyuanyun.pic.ctrl.service.ImportService#saveImportData(List, Class)}方法执行之后织入，进行消息推送
     * @param
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-10-11
     */
    @AfterReturning(value = "importServicePointcut(pushMessage) && execution(* saveImportData(..))", returning = "retVal")
    public void saveImportDataAdvice(JoinPoint joinPoint, PushMessage pushMessage, boolean retVal) {
        try {
            if(retVal) { //当返回值为true才执行以下方法
                String[] values = pushMessage.value();
                if (values.length > 0) {
                    SpelExpressionParser parser = new SpelExpressionParser();
                    EvaluationContext context = super.getEvaluationContext(joinPoint);
                    List data = parser.parseExpression(values[0]).getValue(context, List.class);
                    Class cls = parser.parseExpression(values[1]).getValue(context, Class.class);
                    if(cls != null && data != null && !data.isEmpty()) {
                        ctrlExcelDataInputPushMsg(data, cls);
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.error("外检数据导入消息推送异常", e);
        }
    }

    /**
     * excel数据录入消息推送
     * @param data
     * @param cls
     * @return 
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-10-21
     */
    private void ctrlExcelDataInputPushMsg(List data, Class cls) throws Exception {
        AbstractPushMsgTemplate pushMsgTemplate = null;
        Map map = new HashMap();

        if(PicCtrlExinspCptestStationExcel.class.equals(cls)) { //测试桩检测数据导入消息推送
            pushMsgTemplate = new ExinspCptestStationPushMsg();
        } else if(PicCtrlExinspAcMiorRecordExcel.class.equals(cls) || PicCtrlExinspDcMiorRecordExcel.class.equals(cls)) {
            if(PicCtrlExinspAcMiorRecordExcel.class.equals(cls)) { //交流干扰监测记录数据录入消息推送
                map.put(CtrlPushMsgCons.TYPE_KEY, ExinspAcDcMiorRecordPushMsg.AcDcMiorEnum.AC.toString());
            }
            if(PicCtrlExinspDcMiorRecordExcel.class.equals(cls)) { //直流干扰监测记录数据录入消息推送
                map.put(CtrlPushMsgCons.TYPE_KEY, ExinspAcDcMiorRecordPushMsg.AcDcMiorEnum.DC.toString());
            }
            pushMsgTemplate = new ExinspAcDcMiorRecordPushMsg();
        } else if(PicCtrlExinspInsedJointExcel.class.equals(cls) || PicCtrlExinspSaalAnodeExcel.class.equals(cls)) {
            if (PicCtrlExinspInsedJointExcel.class.equals(cls)) { //缘装置检测记录数据录入消息推送
                map.put(CtrlPushMsgCons.TYPE_KEY, ExinspInsedJointSaalAnodePushMsg.InsedJointSaalAnodeEnum.INSEDJOINT.toString());
            }
            if (PicCtrlExinspSaalAnodeExcel.class.equals(cls)) { ////牺牲阳极检测记录数据录入消息推送
                map.put(CtrlPushMsgCons.TYPE_KEY, ExinspInsedJointSaalAnodePushMsg.InsedJointSaalAnodeEnum.SAALANODE.toString());
            }
            pushMsgTemplate = new ExinspInsedJointSaalAnodePushMsg();
        } else if(PicCtrlExinspOcgradeExcel.class.equals(cls)) { //防腐层等级结果录入消息推送
            pushMsgTemplate = new ExinspOcgradePushMsg();
        } else if(PicCtrlExinspOclpointExcel.class.equals(cls)) { //外涂层漏损点信息录入任务调度消息推送
            pushMsgTemplate = new ExinspOclpointAddNotice();
        }
        if(pushMsgTemplate != null) {
            map.put(CtrlPushMsgCons.LIST_KEY, data);
            pushMsgTemplate.setInterface(false, map);
            pushMsgTemplate.pushMsg();
        }
    }

}
